package com.example.mat.util;

import com.example.mat.entity.MultipleChoice;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 孙宏宇
 * @date 2021/3/30 10:55
 */
public class McExamUtil {

    public static final String ANS_ERROR = "skins/images/error.png";
    public static final String ANS_PASS = "skins/images/success.png";


    public static List<MultipleChoice> getAnswerList(String html) {
        Document doc = Jsoup.parse(html);
        Elements sections = doc.select(".tm_paper_section");

        List<MultipleChoice> result = new ArrayList<MultipleChoice>();


        // 遍历所有题型
        for (Element section : sections) {
            Elements allQuestions = section.select(".tm_paper_question");

            for (Element element : allQuestions) {
                // 题目
                String title = element.select(".tm_question_lineheight").get(1).text().trim();
                // 拿到li 列表
                Elements lis = element.getElementsByTag("li");
                // 判断回答是否正确
                String imgSrc = lis.get(0).getElementsByTag("img").get(0).attr("src");
                if (imgSrc.equals(ANS_PASS)) {
                    // 本题回答正确
                    System.out.println("--> 本题回答正确");

                    Element tbody = element.getElementsByTag("tbody").get(0);

                    // 获取选项
                    List<String> ansList = new ArrayList<String>();
                    Elements ansLis = tbody.select("label");
                    for (Element li : ansLis) {
                        String line = li.text();
                        line = line.substring(line.indexOf('.') + 1).trim();
                        ansList.add(line);
                    }

                    // 组建对象
                    MultipleChoice multipleChoice = new MultipleChoice();
                    multipleChoice.setTitle(title);
                    multipleChoice.setOptions(ansList);

                    // 获取答案
                    String p = tbody.select("p").get(0).text().trim();
                    // 判断是不是多选题
                    if (p.length() > 1) {
                        // 多选题
                        System.out.println("是多选题");
                        multipleChoice.setMultipleChoice(true);
                        StringBuilder choices = new StringBuilder();
                        for (int i = 0; i < p.length(); i++) {
                            if (i > 0) choices.append('&');
                            choices.append(p.charAt(i) - 'A');
                        }
                        multipleChoice.setAnswer(choices.toString());
                    } else {
                        // 单选题
                        multipleChoice.setMultipleChoice(false);
                        char choice = p.charAt(0);
                        multipleChoice.setAnswer(String.valueOf(choice - 'A'));
                    }

                    result.add(multipleChoice);
                    System.out.println(multipleChoice);

                } else if (imgSrc.equals(ANS_ERROR)) {
                    // 本题回答错误
                    System.out.println("--> 本题回答错误");
                } else {
                    throw new RuntimeException("无法判断改题的正确性");
                }

            }

        }

        return result;
    }


    public static String getThieExamUri(String html) {
        Document doc = Jsoup.parse(html);
        Element table = doc.select(".tm_table_list").get(0);
        Element tbody = table.getElementsByTag("tbody").get(0);
        Element tr1 = tbody.getElementsByTag("tr").get(0);
        Element a = tr1.select("a").get(0);
        String href = a.attr("href");
        return href;
    }

}
